//
// [Author] lg (https://github.com/lemos1235)
// [Date] 2023/1/18
//
import 'package:camera/camera.dart';
import 'package:flutter/material.dart';

class FlashlightButton extends StatefulWidget {
  const FlashlightButton({Key? key, required this.cameraController}) : super(key: key);

  final CameraController cameraController;

  @override
  State<FlashlightButton> createState() => _FlashlightButtonState();
}

class _FlashlightButtonState extends State<FlashlightButton> {
  FlashMode flashMode = FlashMode.auto;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        switch (flashMode) {
          case FlashMode.auto:
            flashMode = FlashMode.off;
            break;
          case FlashMode.off:
            flashMode = FlashMode.torch;
            break;
          default:
            flashMode = FlashMode.auto;
        }
        widget.cameraController.setFlashMode(flashMode);
        setState(() {});
      },
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Icon(
            flashMode == FlashMode.auto
                ? Icons.flash_auto
                : flashMode == FlashMode.off
                    ? Icons.flash_off
                    : Icons.flash_on,
            color: Colors.white,
            size: 30,
          ),
          SizedBox(height: 8),
          Text(
            flashMode == FlashMode.auto
                ? "自动"
                : flashMode == FlashMode.off
                    ? "关闭"
                    : "点亮",
            style: TextStyle(color: Colors.white, fontSize: 12),
          ),
        ],
      ),
    );
  }
}
